package com.vector.ding.web.controller.bizifo;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vector.ding.commons.ActionResult;
import com.vector.ding.commons.Constant;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.file.UploadUtil;
import com.vector.ding.commons.util.front.BuiUtil;
import com.vector.ding.dao.mybatis.model.GqtzgsBaseInfo;
import com.vector.ding.dao.mybatis.model.GqtzgsBizInfo;
import com.vector.ding.dao.mybatis.model.ZmjgBaseInfo;
import com.vector.ding.web.service.baseinfo.GqtzgsBaseInfoMgrService;
import com.vector.ding.web.service.baseinfo.YzgsBaseInfoMgrService;
import com.vector.ding.web.service.bizinfo.GqtzgsBizInfoMgrService;


@Controller
@RequestMapping("/bizinfo/gqtzgs")
public class GqtzgsBizInfoMgrController {
	
	@Autowired
	private GqtzgsBizInfoMgrService service;
	
	@Autowired
	private GqtzgsBaseInfoMgrService baseService;
	
	@Autowired
	private YzgsBaseInfoMgrService yzgsBaseService;

	@RequestMapping(value = "/mgrpage.do", method = RequestMethod.GET)
	public ModelAndView getMgrPage() {
		return new ModelAndView("/bizinfo/gqtzgs_biz_info_mgr");
	}
	
	@ResponseBody
	@RequestMapping(value = "/getdata.do", method = RequestMethod.POST)
	public JSONObject getData(HttpServletRequest request, 
			int pageIndex, int limit) {
		List<GqtzgsBizInfo> data = service.getBizInfo(pageIndex + 1, limit, request);
	
		return BuiUtil.gridAdapter(data);
	}
	
	@ResponseBody
	@RequestMapping(value = "/baseCompanyList.do", method = RequestMethod.POST)
	public JSONArray getBaseCompanyList(HttpServletRequest request) {
		List<GqtzgsBaseInfo> data = baseService.getBaseInfo(0, Integer.MAX_VALUE, request);
		JSONArray result = new JSONArray();
		for (GqtzgsBaseInfo baseInfo: data) {
			JSONObject json = new JSONObject();
			json.put("value", baseInfo.getNumber());
			json.put("text", baseInfo.getCompanyName());
			result.add(json);
		}
		return result;
//		return JSONArray.fromObject(data);
//		return BuiUtil.gridAdapter(data);
	}
	
	@ResponseBody
	@RequestMapping(value = "/selectCompany.do", method = RequestMethod.POST)
	public JSONArray selectCompany(HttpServletRequest request) {
		List<GqtzgsBaseInfo> data = baseService.getBaseInfo(0, Constant.SUGECT_NUM, request);
		JSONArray result = new JSONArray();
		for (GqtzgsBaseInfo baseInfo: data) {
			/*JSONObject json = new JSONObject();
			json.put("value", baseInfo.getNumber());
			json.put("text", baseInfo.getCompanyName());
			result.add(json);*/
			result.add(StringUtil.getString(baseInfo.getCompanyName()));
			if (result.size() >= Constant.SUGECT_NUM)
				return result;
		}
		return result;
//		return JSONArray.fromObject(data);
//		return BuiUtil.gridAdapter(data);
	}
	
	@ResponseBody
	@RequestMapping(value = "/selectGreatOrg.do", method = RequestMethod.POST)
	public JSONArray selectGreatOrg(HttpServletRequest request, String greatOrg1Name, 
			String grateOrg2Name) {
		String nameLike = StringUtil.getString(greatOrg1Name);
		if (StringUtil.isEmpty(nameLike))
			nameLike = StringUtil.getString(grateOrg2Name);
		List<ZmjgBaseInfo> data = yzgsBaseService.getRecords(0, Constant.SUGECT_NUM, nameLike);
		JSONArray result = new JSONArray();
		for (ZmjgBaseInfo baseInfo: data) {
			result.add(StringUtil.getString(baseInfo.getCompanyName()));
			if (result.size() >= Constant.SUGECT_NUM)
				return result;
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="editData.do", method = RequestMethod.POST)
	public JSONObject editData(String action, HttpServletRequest request) {
		boolean operationResult = true;
		if (Constant.RECORD_ADD.equals(action)) {
			/*String comName = request.getParameter("companyName");
			GqtzgsBizInfo checkInfo = service.getBizInfo(comName,0);
			if(checkInfo != null){
				return ActionResult.fail("所添加公司已经存在！");
			}*/
			GqtzgsBizInfo record = service.parseBizInfo(request);
			operationResult = service.addBizInfo(record);
		} else if (Constant.RECORD_UPDATE.equals(action)) {
			GqtzgsBizInfo record = service.parseBizInfo(request);
			operationResult = service.updateBizInfo(record);
		} else if (Constant.RECORD_DELETE.equals(action)) {
			String[] ids = request.getParameterValues("ids[]");
			if (null == ids)
				return ActionResult.success();
			service.deleteBizInfo(ids);
		}
		
		if (operationResult)
			return ActionResult.success();
		return ActionResult.fail("操作失败，请稍后重试！");
	}
	
	@ResponseBody
	@RequestMapping(value = "/importData.do", method = RequestMethod.POST)
	public JSONObject importData(HttpServletRequest request,
			HttpServletResponse response) {
		String uploadPath = Constant.Upload.BIZ_INF_PATH;
		try {
			File file = UploadUtil.getUploadFile(request, response, uploadPath);
			service.importFormFile(file);
			return ActionResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.fail("导入数据失败：" + e.getMessage());
		}
	}
}